package com.wutianyi.controller.innvo;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.wutianyi.module.member.dataobject.MemberDO;
import com.wutianyi.module.product.dataobject.ProductAttrDO;
import com.wutianyi.module.product.dataobject.ProductDO;
import com.wutianyi.module.product.dataobject.ProductDTO;
import com.wutianyi.module.product.dataobject.ProductInfoDO;
import com.wutianyi.module.services.ProductServices;
import com.wutianyi.utils.MyHttpClient;
import com.wutianyi.utils.Utils;

@Controller
public class ProductController {
	
	@Autowired
	private ProductServices productServices;
	
	private MyHttpClient httpClient = new MyHttpClient();
	
	@RequestMapping("/innov/product_add.do")
	public ModelAndView addProduct() {
		ModelAndView modelAndView = new ModelAndView("innov/product_add");
		
		return modelAndView;
	}
	
	@RequestMapping(value = "/innov/product_add.do", method = RequestMethod.POST)
	public ModelAndView addProduct(@RequestParam(defaultValue = "0") int id, String pname,
			String pdescription, @RequestParam("product_attrs") String productAttrs, String image,
			String productinfo) {
		ModelAndView modelAndView = new ModelAndView("innov/product_edit");
		if (StringUtils.isBlank(image)) {
			image = "http://blog.karstock.net/serve.do?key=aa9785581d43e86f1bb85e7962330e01";
		}
		
		MemberDO member = (MemberDO) SecurityUtils.getSubject().getPrincipal();
		
		ProductDO product = new ProductDO(member.getcId(), pname, image, pdescription);
		List<ProductAttrDO> pas = new ArrayList<ProductAttrDO>();
		if (StringUtils.isNotBlank(productAttrs)) {
			String[] attrs = productAttrs.split("@;@");
			for (String attr : attrs) {
				String[] as = attr.split("@#@");
				if (as.length == 2) {
					pas.add(new ProductAttrDO(0, as[0], as[1]));
				}
			}
		}
		ProductDTO productDTO = new ProductDTO(product, pas, new ProductInfoDO(0, productinfo));
		
		if (id != 0) {
			productDTO.setId(id);
			product.setId(id);
			productServices.updateProduct(productDTO);
		} else {
			productServices.createProduct(productDTO);
		}
		modelAndView.addObject("product", productServices.getProductDTO(member.getcId(), id));
		
		return modelAndView;
	}
	
	@RequestMapping("/innov/product_list_json.do")
	public ResponseEntity<String> listProductsJson(@RequestParam(defaultValue = "1") int pageNo,
			@RequestParam(defaultValue = "20") int pageSize) {
		MemberDO member = (MemberDO) SecurityUtils.getSubject().getPrincipal();
		List<ProductDO> products = productServices.listProductsByCompanyId(member.getcId(), pageNo,
				pageSize);
		
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		
		return Utils.createJsonResponse(0, "success", products);
	}
	
	@RequestMapping("/innov/product_list.do")
	public ModelAndView listProducts(@RequestParam(defaultValue = "1") int pageNo,
			@RequestParam(defaultValue = "5") int pageSize) {
		ModelAndView modelAndView = new ModelAndView("innov/product_list");
		MemberDO member = (MemberDO) SecurityUtils.getSubject().getPrincipal();
		
		int count = productServices.listProductCountByCompanyId(member.getcId());
		List<ProductDO> products = productServices.listProductsByCompanyId(member.getcId(), pageNo,
				pageSize);
		
		modelAndView.addObject("count", count);
		modelAndView.addObject("totalPage", count % pageSize == 0 ? count / pageSize : count
				/ pageSize + 1);
		modelAndView.addObject("pageNo", pageNo);
		modelAndView.addObject("pageSize", pageSize);
		modelAndView.addObject("products", products);
		
		return modelAndView;
	}
	
	@RequestMapping("/innov/product_images_manager.do")
	public ModelAndView productImagesManager() {
		ModelAndView modelAndView = new ModelAndView("innov/product_images_manager");
		
		return modelAndView;
	}
	
	
	@RequestMapping("/innov/product_edit.do")
	public ModelAndView productEdit(@RequestParam(defaultValue = "0") int id) {
		ModelAndView modelAndView = new ModelAndView("innov/product_edit");
		
		MemberDO member = (MemberDO) SecurityUtils.getSubject().getPrincipal();
		ProductDTO product = productServices.getProductDTO(member.getcId(), id);
		modelAndView.addObject("product", product);
		return modelAndView;
	}
	
	@RequestMapping("/list_file.do")
	public ResponseEntity<String> listFiles() {
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		return new ResponseEntity<String>(
				httpClient.getBodyAsString("http://localhost:8888/list_file.do"), headers,
				HttpStatus.OK);
	}
	
	@RequestMapping("/serve.jpg")
	public ResponseEntity<byte[]> serve(String key,
			@RequestParam(value = "width", defaultValue = "40") int width,
			@RequestParam(value = "height", defaultValue = "40") int height) {
		byte[] bs = httpClient.getBody("http://localhost:8888/serve.do?key=" + key + "&width="
				+ width + "&height=" + height);
		HttpHeaders headers = new HttpHeaders();
		headers.setContentType(MediaType.IMAGE_JPEG);
		return new ResponseEntity<byte[]>(bs, headers, HttpStatus.OK);
	}
}
